const app = getApp();
import { sendAction } from '../../../utils/sendAction';
Page({
  /**
   * 页面的初始数据
   */
  data: {
    openid: "",
    id_card_front: "",
    id_card_back: "",
    student_card: "",
    other_img: ""
  },
  onLoad() {
    this.getCertificate()
  },
  async getCertificate(){
    const res = await sendAction("getCertificate", { openid: app.openid }, 'POST');
    const { id_card_front, id_card_back, student_card, other_img } = res.data.data[0];
    this.setData({
      id_card_front, id_card_back, student_card, other_img
    })
  },
  handleAddPhoto(e) {
    const _this=this
    wx.chooseImage({
      success(res) {
        const tempFilePaths = res.tempFilePaths
        wx.uploadFile({
          url: app.baseUrl + `uploadCertificate`, 
          filePath: tempFilePaths[0],
          name: 'file',
          formData: {
            'user': 'test'
          },
          success(res) {
            const data = JSON.parse(res.data).data
            
            if (e.currentTarget.id == "add1") {
              _this.setData({
                id_card_front: data
              })
            }
            if (e.currentTarget.id == "add2") {
              _this.setData({
                id_card_back: data
              })
            }
            if (e.currentTarget.id == "add3") {
              _this.setData({
                student_card: data
              })
            }
            if (e.currentTarget.id == "add4") {
              _this.setData({
                other_img: data
              })
            }
          }
        })
      }
    })
  },
  handlePush: async function () {
    const {id_card_front, id_card_back, student_card, other_img } = this.data;
    if(!id_card_front)return app.showModal({content:`请完善 身份证信息面`})
    if(!id_card_back)return app.showModal({content:`请完善 身份证国徽`})
    if(!student_card)return app.showModal({content:`请完善 一卡通信息面`})
    const res = await app.request({url:`updateUser`,data:{ openid:app.openid, id_card_front, id_card_back, student_card, other_img,status:`待审核` },method:"POST"});
    if (res.meta.status>=300) return app.showModal({content:`上传证件失败：`+res.meta.msg})
    app.showModal({content:`上传证件成功，待审核`})
    
  }
})